layui.use(['jquery','element','upload','laydate','layer','form'],function(){
    var $=layui.jquery;
    var laydate=layui.laydate;
    var upload=layui.upload;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    laydate.render({
        elem: '#birthDate',
        type: 'datetime',
        trigger: 'click'
    })

    laydate.render({
        elem: '#registerDate',
        type: 'datetime',
        trigger: 'click'
    })
    var imgFiles;
    var imgIndex;
    var imgUploadCount = 0;
    upload.render({
        elem:"#userHeadPortrait",
        url:"/login/uploadHeadPortrait",
        field:'headPortrait',
        accept:'images',
        size: 2048,
        before:function(obj){
            imgUploadCount++;
            obj.preview(function(index, file, result){
                imgIndex = index;
                $('#userHeadPortrait').attr('src',result);
            });
            if(imgUploadCount != 1){
                delete imgFiles[imgIndex];
            }
            imgFiles = obj.pushFile();
            element.progress('imgProgress', '99%');
        },
        done:function(res){
            $("#acatarUrl").val(res.data.src);
            element.progress('imgProgress', '100%');
            layer.msg('头像上传成功');
        }
    })
    function getJsUsersAccount() {
        var usersAccount = new Array();
        $.ajax({
            url: "/login/getJsUserAccount",
            dataType: "json",
            async: false,
            success:function (data) {
                usersAccount = data.data;
            }
        })
        return usersAccount;
    }

    form.on('submit(updateUser)',function (obj) {
        $.ajax({
            url:"/login/updateJsUser",
            data:obj.field,
            type:'post',
            dataType:'json',
            success:function (data) {
                var user = JSON.parse(localStorage.getItem('user'));
                if(user.data.userId == $('#userId').val()){
                    parent.parent.$('#headPortrait').attr('src',obj.field.acatarUrl);
                }
                var layer = parent.layer;
                layer.close(layer.index);
            }
        })
        return false;
    });
    //关闭按钮
    $('#close').click(function () {
        var layer = parent.layer;
        layer.close(layer.index);
    });
})